package jdbc;

import java.sql.*;
import java.util.Scanner;
import java.util.prefs.Preferences;

public class JdbcReg {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();){
            Scanner scanner = new Scanner(System.in);
            System.out.println("用户名字");
            String username = scanner.nextLine();
            System.out.println("用户密码");
            String password = scanner.nextLine();
            System.out.println("用户昵称");
            String nickname = scanner.nextLine();
            System.out.println("用户年龄");
            Integer age = scanner.nextInt();

            String sql1 = "select id from userinfo where username=?";
            PreparedStatement pr2 = connection.prepareStatement(sql1);
            pr2.setString(1, username);
            ResultSet rs = pr2.executeQuery();
            if(rs.next()){
                System.out.println("用户名已被占用");
            }else{
                String sql2 = "insert into userinfo(username,password,nickname,age) values(?,?,?,?) ";
                PreparedStatement s1 = connection.prepareStatement(sql2);
                s1.setString(1, username);
                s1.setString(2, password);
                s1.setString(3, nickname);
                s1.setInt(4,age);
                int i = s1.executeUpdate();
                if(i>0){
                    System.out.println("恭喜注册成功");
                }else {
                    System.out.println("系统繁忙稍后再试");
                }

            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
